package com.heima.behavior.service.impl;

import com.heima.behavior.dtos.UnLikesBehaviorDto;
import com.heima.behavior.pojos.ApUnlikesBehavior;
import com.heima.behavior.service.ApUnlikesBehaviorService;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.pojos.ApUser;
import com.heima.utils.thread.ApThreadLocalUtil;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class ApUnlikesBehaviorServiceImpl implements ApUnlikesBehaviorService {

    @Autowired
    private MongoTemplate mongoTemplate;
    @Autowired
    private StringRedisTemplate redisTemplate;

    /**
     * 存储不喜欢的数据
     * @param dto
     * @return
     */
    @Override
    public ResponseResult unLikes(UnLikesBehaviorDto dto) {
        //1.参数校验
        if (dto.getArticleId() == null ) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //获取登录用户id
        ApUser user = ApThreadLocalUtil.getUser();
        if (user == null) {
            //表示未登录
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }
        //游客身份不能进行此操作
        if (user.getId() == 0) {
            return ResponseResult.okResult(405,"游客身份不能进行此操作");
        }
        //2.查询当前用户是否已经点过不喜欢
        //定义redis的key
        String unLikes_key="USER_UNLIKES_"+dto.getArticleId();
        boolean flag=redisTemplate.opsForHash().hasKey(unLikes_key,user.getId()+"");
        //3.如果已经点不喜欢，并且正在执行不喜欢动作，直接返回
        if (flag && dto.getType()==0){
            return ResponseResult.okResult(AppHttpCodeEnum.DATA_EXIST);
        }
        //4.如果已经点不喜欢，并且正在执行取消不喜欢动作
        if (flag && dto.getType()==1){
            //删除不喜欢的数据
            redisTemplate.opsForHash().delete(unLikes_key,user.getId()+"");
            mongoTemplate.remove(Query.query(Criteria.where("userId").is(user.getId()).and("articleId").is(dto.getArticleId())), ApUnlikesBehavior.class);
            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        }
        //5.如果没有点不喜欢,并且现在执行不喜欢动作
        if (!flag && dto.getType()==0){
            //插入不喜欢的数据
            redisTemplate.opsForHash().put(unLikes_key,user.getId()+"","1");
            ApUnlikesBehavior unlikesBehavior=new ApUnlikesBehavior();
            unlikesBehavior.setId(ObjectId.get().toHexString());
            unlikesBehavior.setUserId(user.getId().longValue());
            unlikesBehavior.setArticleId(dto.getArticleId());
            unlikesBehavior.setType((short) 0);
            unlikesBehavior.setCreatedTime(new Date());
            mongoTemplate.insert(unlikesBehavior);
            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        }
        //以上都不是,就返回数据
        return null;
    }
}
